package com.xzy.controller;

import com.xzy.config.Audience;
import com.xzy.entity.Hole;
import com.xzy.service.HoleService;
import com.xzy.utils.FileUtils;
import com.xzy.utils.JSONResult;
import com.xzy.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@ResponseBody
public class HoleController {
    @Autowired
    private HoleService holeService;
    @Autowired
    private Audience audience;

    @PostMapping("/question/hole/add")
    public JSONResult add(Hole hole, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String _userId = JWTUtils.getUserId(token, audience.getBase64Secret());
        int userId = Integer.parseInt(_userId);
        hole.setUserId(userId);
        String text = request.getParameter("holeContent");
        String url= FileUtils.getUrl(text,userId);
        hole.setHoleContent(url);
        holeService.addHole(hole);
        String content=FileUtils.getContent(url);
        hole.setHoleContent(content);
     //   Map<String, Object> map = new HashMap<String, Object>();
      //  map.put("hole", hole);
        return JSONResult.SUCCESS(hole);
    }

    @GetMapping("/question/hole/getHole/{holeId}")
    public JSONResult getHole(@PathVariable("holeId") int holeId) {
        Hole hole = holeService.getHoleById(holeId);
        String content = FileUtils.getContent(hole.getHoleContent());
        hole.setHoleContent(content);
        return JSONResult.SUCCESS(hole);
    }

    @GetMapping("/question/hole/findAll")
    public JSONResult findAll() {
        List<Hole> holes = holeService.findAll();
        for(Hole hole:holes)
        {
            hole.setHoleContent(FileUtils.getContent(hole.getHoleContent()));
        }
        return JSONResult.SUCCESS(holes);
    }

    @PostMapping("/question/hole/getHoleByType")
    public JSONResult getHoleByType(@RequestParam("holeType") String holeType) {
        List<Hole> holes = holeService.getHoleByType(holeType);
        for(Hole hole:holes)
        {
            hole.setHoleContent(FileUtils.getContent(hole.getHoleContent()));
        }
        return JSONResult.SUCCESS(holes);
    }

    @PutMapping("/question/hole/updateHole/{holeId}")
    public JSONResult updateHole(Hole hole,@PathVariable("holeId") int holeId)
    {
                Hole o_Hole=holeService.getHoleById(holeId);
                hole.setHoleId(o_Hole.getHoleId());
                hole.setUserId(o_Hole.getUserId());
                String text=hole.getHoleContent();
                String url=FileUtils.getUrl(text,hole.getUserId());
                hole.setHoleContent(url);
                holeService.updateHole(hole);
                String content=FileUtils.getContent(url);
                hole.setHoleContent(content);
                return JSONResult.SUCCESS(hole);
    }

    @DeleteMapping("/question/hole/deleteHole/{holeId}")
    public JSONResult deleteHole(@PathVariable("holeId") int holeId)
    {
         Hole hole=holeService.getHoleById(holeId);
         holeService.deleteHole(holeId);
         return JSONResult.SUCCESS(hole);
    }

}





